<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>PC站推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>Genres</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        class="exclusive"
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="rowSave"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";

import { pcCategory as baseURL, uploadFile } from "@/api/cat/baseUrls";
import { bookCategory as bookURL } from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      loading: false,
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "create_time&DESC",
      },
      option: {
        index: true,
        indexLabel: "编号",
        excelBtn: false,
        border: true,
        dialogModal: true,
        tip: true,
        dialogClickModal: false,
        column: [
          {
            label: "偏好",
            type: 'select',
            prop: "preference",
            dicData: [
              {
                label: "男频",
                value: 1,
              },
              {
                label: "女频",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "偏好必须填写",
              },
            ],
            search: true,
            change:(val)=>{
              console.log(this.option.column[1]);
              this.getCategoryList(val.value)
            },
          },
          {
            label: "分类名称",
            prop: "categoryName",
            type: "select",
            props: {
              label: "name",
              value: "category_id",
            },
            dicData: [], 
            span: 24,
            row: true,
            rules: [
              {
                required: true,
                message: "分类名称必须填写",
              },
            ],
          },
          {
            label: "分类图",
            type: "upload",
            span: 24,
            display: true,
            showFileList: true,
            multiple: false,
            limit: 10,
            propsHttp: {},
            canvasOption: {},
            prop: "img_url",
            drag: false,
            listType: "picture-img",
            fileSize: 10240,
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "分类图必须上传",
              },
            ],
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            search: false,
            placeholder: "排序",
            rules: [
              {
                required: true,
                message: "请输入排序",
              },
            ],
          },
          {
            label: "状态",
            type: 'select',
            prop: "status",
            dicData: [
              {
                label: "上线",
                value: 0,
              },
              {
                label: "下线",
                value: 1,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "状态必须填写",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "edit_account_name",
            addDisplay: false,
            editDisplay: false
          },
        ],
      },
    };
  },
  methods: {

    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.page,
        { ...this.params },
        this.page,
        "GET"
      );
      this.data = response.data || [];
      this.loading = false;
      this.getCategoryList()
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      let data = {
        id: row.id,
        preference: row.preference,
        img_url: row.img_url,
        sort: row.sort,
        status: row.status,
        category_id: row.category_id,
        edit_account_id: userInfo.id,
        edit_account_name: userInfo.username,
      }
      let res = await common(baseURL.update, data, null, "PUT");
      if (res == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**新增 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      let data = {
        preference: row.preference,
        category_id: row.categoryName,
        img_url: row.img_url,
        sort: row.sort,
        status: row.status,
        edit_account_id: userInfo.id,
        edit_account_name: userInfo.username,
      }
      let res = await common(baseURL.add, data, null, "POST");
      if (res == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let userInfo = this.$store.state.user.userInfo;
          let response = await common(
            `${baseURL.del}?id=${row.id}&userId=${userInfo.id}&userName=${userInfo.username}`,
            {},
            null,
            "DELETE"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },

    // 获取分类列表
    async getCategoryList(preference) {
      let v = preference ? preference : 1
      let response = await common(
        bookURL.selectByPreference + '/' + v,
        {},
        "GET"
      );
      this.option.column[1].dicData = response.data
    },

  },
};
</script>